package com.winit.openapi.wh.allocation.builder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.allocation.command.order.AllocationRecommendCommand;
import com.winit.oms.spi.allocation.command.order.QueryOrderBySellerCommand;
import com.winit.oms.spi.allocation.vo.AllocationOrderEnumContants.OrderSource;
import com.winit.oms.spi.allocation.vo.order.AllocationOrderFeeEstimateVo;
import com.winit.oms.spi.allocation.vo.order.AllocationOrderMerchandiseVo;
import com.winit.oms.spi.allocation.vo.order.AllocationOrderSlaVo;
import com.winit.oms.spi.allocation.vo.order.AllocationOrderVo;
import com.winit.oms.spi.allocation.vo.order.AllocationRecommendVo;
import com.winit.oms.spi.vo.v2.order.CustomerInfo;
import com.winit.oms.spi.vo.v2.order.ProductInfo;
import com.winit.oms.spi.vo.v2.order.WarehouseInfo;
import com.winit.oms.spi.vo.v2.response.OrderSubmitResultMerchandiseVo;
import com.winit.oms.spi.vo.v2.response.OrderSubmitResultOrderVo;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.wh.allocation.vaildator.AllocationOrderfieldValidator;
import com.winit.openapi.wh.utils.JsonValid;

public class AllocationOrderBuilder {

	public static QueryOrderBySellerCommand buildAllocationCommand(
			JSONObject json) {
		QueryOrderBySellerCommand command = new QueryOrderBySellerCommand();
		AllocationOrderVo vo = new AllocationOrderVo();
		// command构建
		if (json.containsKey("orderNo")) {
			vo.setOrderNo(json.getString("orderNo"));
		}
		ProductInfo info = new ProductInfo();
		if (json.containsKey("winitProductCode")) {//
			info.setWinitProductCode(json.getString("winitProductCode"));
		}
		if (json.containsKey("winitProductCategory2")) {//
			info.setWinitProductCategory2(json
					.getString("winitProductCategory2"));
		}
		if (json.containsKey("winitProductCategory3")) {//
			info.setWinitProductCategory3(json
					.getString("winitProductCategory3"));
		}
		vo.setProductInfo(info);
		List<AllocationOrderMerchandiseVo> mList = new ArrayList<AllocationOrderMerchandiseVo>();
		AllocationOrderMerchandiseVo mvo = new AllocationOrderMerchandiseVo();
		if (json.containsKey("merchandiseCode")) {// 商品编码
			mvo.setMerchandiseCode(json.getString("merchandiseCode"));
		}
		if (json.containsKey("merchandiseName")) {// 商品名称
			mvo.setNameCn(json.getString("merchandiseName"));
		}
		mList.add(mvo);
		vo.setAllocationOrderMerchandiseVos(mList);
		WarehouseInfo disWh = new WarehouseInfo();
		if (json.containsKey("dispatchWhCode")) {//
			disWh.setWarehouseCode((json.getString("dispatchWhCode")));
		}
		vo.setDispatchWarehouseInfo(disWh);
		WarehouseInfo desWh = new WarehouseInfo();
		if (json.containsKey("destinationWhCode")) {//
			desWh.setWarehouseCode(json.getString("destinationWhCode"));
		}
		if (json.containsKey("orderStatus")) {//
			vo.setStatus(json.getString("orderStatus"));
		}
		vo.setDestinationWarehouseInfo(desWh);
		if (json.containsKey("serialNumber")) {//
			vo.setSerialNumber(json.getString("serialNumber"));
		}
		if (json.containsKey("orderSource")) {//
			vo.setOrderSource(json.getString("orderSource"));
		}
		if (json.containsKey("orderTime")) {//
			vo.setOrderTime(json.getDate("orderTime"));
		}
		if (json.containsKey("estimateItemQty")) {//
			vo.setEstimateItemQty(json.getInteger("estimateItemQty"));
		}
		if (json.containsKey("estimateMerchandiseQty")) {//
			vo.setEstimateMerchandiseQty(json
					.getInteger("estimateMerchandiseQty"));
		}
		if (json.containsKey("estimatePackageQty")) {//
			vo.setEstimatePackageQty(json.getInteger("estimatePackageQty"));
		}
		if (json.containsKey("estimateContainerQty")) {//
			vo.setEstimateContainerQty(json.getInteger("estimateContainerQty"));
		}
		if (json.containsKey("actualContainerQty")) {//
			vo.setActualContainerQty(json.getInteger("actualContainerQty"));
		}
		if (json.containsKey("actualPackageQty")) {//
			vo.setActualPackageQty(json.getInteger("actualPackageQty"));
		}
		if (json.containsKey("actualMerchandiseQty")) {//
			vo.setActualMerchandiseQty(json.getInteger("actualMerchandiseQty"));
		}
		if (json.containsKey("actualItemQty")) {//
			vo.setActualItemQty(json.getInteger("actualItemQty"));
		}
		if (json.containsKey("finishedTime")) {//
			vo.setFinishedTime(json.getDate("finishedTime"));
		}
		if (json.containsKey("serviceNormDays")) {//
			vo.setServiceNormDays(json.getBigDecimal("serviceNormDays"));
		}
		if (json.containsKey("serviceCompletionDays")) {//
			vo.setServiceCompletionDays(json
					.getBigDecimal("serviceCompletionDays"));
		}
		if (json.containsKey("orderType")) {// 订单类型
			/*
			 * ProductInfo productInfo = new ProductInfo();
			 * productInfo.setWinitProductCategory2
			 * (json.getString("orderType")); vo.setProductInfo(productInfo);
			 */
			vo.setOrderType(json.getString("orderType"));
		}
		// 转为时间类型.
		if (json.containsKey("orderStartTime")) {// 开始时间
			String orderStartTime = json.getString("orderStartTime");
			orderStartTime += " 00:00:00";
			Date from = DateUtil.parse2Date(orderStartTime,
					DateUtil.FULL_DATE_STRING);
			vo.setOrderStartDate(from);
		}
		if (json.containsKey("orderEndTime")) {// 结束时间
			String orderEndTime = json.getString("orderEndTime");
			orderEndTime += " 23:59:59";
			Date end = DateUtil.parse2Date(orderEndTime,
					DateUtil.FULL_DATE_STRING);
			vo.setOrderEndDate(end);
		}
		command.setOrderVo(vo);
		return command;
	}

	/**
	 * 构建订单列表
	 * 
	 * @param vo
	 * @return
	 */
	public static Map<String, Object> buildAllocationOrderListPageVo(
			AllocationOrderVo vo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderNo", vo.getOrderNo() == null ? "" : vo.getOrderNo());// 订单编号.
		map.put("orderStatus", vo.getStatus() == null ? "" : vo.getStatus());// 订单状态.
		map.put("orderTime", vo.getOrderTime() == null ? "" : vo.getOrderTime());// 下单时间
		map.put("orderSource",
				vo.getOrderSource() == null ? "" : vo.getOrderSource());// 订单来源
		map.put("esVolume", vo.getEstimateVolume() == null ? "" : vo
				.getEstimateVolume().toPlainString());
		map.put("acVolume", vo.getActualVolume() == null ? "" : vo
				.getActualVolume().toPlainString());
		map.put("esWeight", vo.getEstimateWeight() == null ? "" : vo
				.getEstimateWeight().toPlainString());
		map.put("acWeight", vo.getActualWeight() == null ? "" : vo
				.getActualWeight().toPlainString());
		map.put("esChargingweight", vo.getEstimateChargeWeight() == null ? ""
				: vo.getEstimateChargeWeight().toPlainString());
		map.put("acChargingweight", vo.getActualChargeWeight() == null ? ""
				: vo.getActualChargeWeight().toPlainString());
		map.put("ontimeTrack", vo.getOntimeTrack());// 准时派送.
		map.put("serviceNormDay", vo.getServiceNormDays());// 服务标准天数
		map.put("serviceCompletionDay", vo.getServiceCompletionDays());// 服务完成天数
		map.put("esItemNumber",
				vo.getEstimateItemQty() == null ? "" : vo.getEstimateItemQty());
		map.put("asItemNumber",
				vo.getActualItemQty() == null ? "" : vo.getActualItemQty());
		map.put("voidTime", vo.getVoidTime()==null?"":vo.getVoidTime());
		map.put("shelfCompletionTime", vo.getFinishedTime()); //服务完成时间
		// 发货仓库信息
		WarehouseInfo disWh = vo.getDispatchWarehouseInfo();
		map.put("dispatchWhCode",
				disWh.getWarehouseCode() == null ? "" : disWh
						.getWarehouseCode());
		map.put("dispatchWhName",
				disWh.getWarehouseName() == null ? "" : disWh
						.getWarehouseName());
		// 目的仓库信息
		WarehouseInfo desWh = vo.getDestinationWarehouseInfo();
		map.put("destinationWhCode", desWh.getWarehouseCode() == null ? ""
				: desWh.getWarehouseCode());
		map.put("destinationWhName", desWh.getWarehouseName() == null ? ""
				: desWh.getWarehouseName());
		// sla
		/*
		 * map.put("ontimeTrack", value); map.put("slaCalType", value);
		 * map.put("serviceNormDay", value); map.put("serviceCompletionDay",
		 * value); map.put("shelfCompletionTime", value);
		 */
		List<Map<String, Object>> merList = new ArrayList<Map<String, Object>>();
		List<AllocationOrderMerchandiseVo> merchandiseInfoList = vo
				.getAllocationOrderMerchandiseVos();
		if (null != merchandiseInfoList && merchandiseInfoList.size() > 0) {
			for (AllocationOrderMerchandiseVo tempVo : merchandiseInfoList) {
				Map<String, Object> merMap = new HashMap<String, Object>();
				merMap.put(
						"merchandiseCode",
						tempVo.getMerchandiseCode() == null ? "" : tempVo
								.getMerchandiseCode());
				merMap.put(
						"specification",
						tempVo.getSpecification() == null ? "" : tempVo
								.getSpecification());
				merMap.put("qty", tempVo.getEstimateDispatchQty() == null ? ""
						: tempVo.getEstimateDispatchQty());
				merList.add(merMap);
			}
		}

		map.put("merList", merList);
		// 产品信息
		ProductInfo productInfo = vo.getProductInfo();
		if (productInfo == null) {
			productInfo = new ProductInfo();
		}
		map.put("orderType",
				productInfo.getWinitProductCategory3() == null ? ""
						: productInfo.getWinitProductCategory3());
		map.put("winitProductName",
				productInfo.getWinitProductName() == null ? "" : productInfo
						.getWinitProductName());// Winit产品名称

		return map;
	}

	public static Map<String, Object> buildReturnAllocationOrderInfo(
			AllocationOrderVo allocationOrderVo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderNo", allocationOrderVo.getOrderNo() == null ? ""
				: allocationOrderVo.getOrderNo());// 订单编号
		String status = allocationOrderVo.getStatus();
		if (StringUtils.isNotBlank(status)) {
			map.put("status", allocationOrderVo.getStatus());// 订单状态
		}
		map.put("dispatchWhCode", allocationOrderVo.getDispatchWarehouseInfo()
				.getWarehouseCode() == null ? "" : allocationOrderVo
				.getDispatchWarehouseInfo().getWarehouseCode());
		map.put("dispatchWhName", allocationOrderVo.getDispatchWarehouseInfo()
				.getWarehouseName() == null ? "" : allocationOrderVo
				.getDispatchWarehouseInfo().getWarehouseName());
		map.put("destinationWhCode", allocationOrderVo
				.getDestinationWarehouseInfo().getWarehouseCode() == null ? ""
				: allocationOrderVo.getDestinationWarehouseInfo()
						.getWarehouseCode());
		map.put("destinationWhName", allocationOrderVo
				.getDestinationWarehouseInfo().getWarehouseName() == null ? ""
				: allocationOrderVo.getDestinationWarehouseInfo()
						.getWarehouseName());
		
		// 如果关联了入库单号
		map.put("inboundOrderNo", allocationOrderVo.getInboundOrderNo());
		
		// 下单时间
		map.put("orderTime", allocationOrderVo.getOrderTime() == null ? ""
				: allocationOrderVo.getOrderTime());
		// 截单时间
		map.put("interceptTime", allocationOrderVo.getInterceptTime());
		map.put("estimatedShelfTime", allocationOrderVo.getEstimatedShelfTime());
		map.put("timeZone",allocationOrderVo.getTimeZone());
		map.put("remark",allocationOrderVo.getRemark());
		map.put("actualMerchandiseQty", allocationOrderVo.getActualMerchandiseQty());
		// 产品信息
		ProductInfo productInfo = allocationOrderVo.getProductInfo();
		if (allocationOrderVo.getProductInfo() != null) {
			map.put("winitProductCode",
					productInfo.getWinitProductCode() == null ? ""
							: productInfo.getWinitProductCode());
			map.put("winitProductName",
					productInfo.getWinitProductName() == null ? ""
							: productInfo.getWinitProductName());
			map.put("exportType", productInfo.getExportType() == null ? ""
					: productInfo.getExportType());// 出库方式
			map.put("orderType",
					productInfo.getWinitProductCategory3() == null ? ""
							: productInfo.getWinitProductCategory3());
		}
		// 商品信息
		List<AllocationOrderMerchandiseVo> infos = allocationOrderVo
				.getAllocationOrderMerchandiseVos();
		List<Map<String, Object>> serviceMapList = new ArrayList<Map<String, Object>>();
		if (infos != null && infos.size() > 0) {
			for (AllocationOrderMerchandiseVo merchandiseInfo : infos) {
				Map<String, Object> serviceMap = new HashMap<String, Object>();
				serviceMap.put("merchandiseCode",
						merchandiseInfo.getMerchandiseCode() == null ? ""
								: merchandiseInfo.getMerchandiseCode());
				serviceMap.put("specification",
						merchandiseInfo.getSpecification() == null ? ""
								: merchandiseInfo.getSpecification());
				serviceMap.put("nameCn",
						merchandiseInfo.getNameCn() == null ? ""
								: merchandiseInfo.getNameCn());
				serviceMap.put("nameEn",
						merchandiseInfo.getNameEn() == null ? ""
								: merchandiseInfo.getNameEn());
				serviceMap.put("length",
						merchandiseInfo.getLength() == null ? ""
								: merchandiseInfo.getLength());
				serviceMap.put("width", merchandiseInfo.getWidth() == null ? ""
						: merchandiseInfo.getWidth());
				serviceMap.put("height",
						merchandiseInfo.getHeight() == null ? ""
								: merchandiseInfo.getHeight());
				serviceMap.put("weight",
						merchandiseInfo.getWeight() == null ? ""
								: merchandiseInfo.getWeight());
				serviceMap.put("volume",
						merchandiseInfo.getVolume() == null ? ""
								: merchandiseInfo.getVolume());
				serviceMap.put("actualWeight",
                    merchandiseInfo.getWeight() == null ? ""
                            : merchandiseInfo.getActualWeight());
				serviceMap.put("actualVolume",
                    merchandiseInfo.getVolume() == null ? ""
                            : merchandiseInfo.getActualVolume());
				serviceMap.put("estimateWeight",
                    merchandiseInfo.getEstimateWeight() == null ? ""
                            : merchandiseInfo.getEstimateWeight());
				serviceMap.put("estimateVolume",
                    merchandiseInfo.getEstimateVolume() == null ? ""
                            : merchandiseInfo.getEstimateVolume());
                serviceMap.put("actualPackageQty",
                    merchandiseInfo.getActualPackageQty() == null ? "" : merchandiseInfo.getActualPackageQty());
				serviceMap.put("itemQty", merchandiseInfo
						.getEstimateDispatchQty() == null ? ""
						: merchandiseInfo.getEstimateDispatchQty());
				serviceMap.put("acItemQty",
						merchandiseInfo.getActualDispatchQty() == null ? ""
								: merchandiseInfo.getActualDispatchQty());
				serviceMap.put("boxQty",
						merchandiseInfo.getBoxQty() == null ? ""
								: merchandiseInfo.getBoxQty());
				serviceMap.put("merchandiseSerno",
						merchandiseInfo.getMerchandiseSerno() == null ? ""
								: merchandiseInfo.getMerchandiseSerno());
				serviceMapList.add(serviceMap);
			}
		}
		map.put("merchandiseList", serviceMapList);

		List<AllocationOrderSlaVo> slaList = allocationOrderVo.getSlaList();
		List<Map<String, Object>> slaMapList = new ArrayList<Map<String, Object>>();
		if (slaList != null && slaList.size() > 0) {
			for (AllocationOrderSlaVo slaVo : slaList) {
				Map<String, Object> slaMap = new HashMap<String, Object>();
				slaMap.put("serviceStatus", slaVo.getStatus() == null ? ""
						: slaVo.getStatus());
				slaMap.put("serviceName", slaVo.getSlaName() == null ? ""
						: slaVo.getSlaName());
				slaMap.put(
						"standardTime",
						slaVo.getServiceStandardTime() == null ? "" : slaVo
								.getServiceStandardTime());
				slaMap.put(
						"completeTime",
						slaVo.getServiceCompletionTime() == null ? "" : slaVo
								.getServiceCompletionTime());
				slaMapList.add(slaMap);
			}
		}
		map.put("serviceList", slaMapList);
		return map;
	}

	/**
	 * 一步下单
	 * 
	 * @param json
	 * @return
	 */
	public static AllocationOrderVo buildVo(JSONObject jsons,
			RequestMsg requestMsg) {
		JsonValid json = new JsonValid(jsons);
		AllocationOrderVo target = new AllocationOrderVo();
		target.setOrderSource(requestMsg.getPlatform());
		// 产品信息
		ProductInfo productInfo = new ProductInfo();
		String orderType = AllocationOrderfieldValidator.getOrderType(json
				.getString("winitProductCode"));
		productInfo.setWinitProductCategory3(orderType);
		productInfo.setWinitProductCode(json.getString("winitProductCode"));
		target.setProductInfo(productInfo);
		// 发货仓库信息
		WarehouseInfo disWh = new WarehouseInfo();
		disWh.setWarehouseCode(json.getString("dispatchWhCode"));
		target.setDispatchWarehouseInfo(disWh);
		// 目的仓库信息
		WarehouseInfo desWh = new WarehouseInfo();
		desWh.setWarehouseCode(json.getString("destinationWhCode"));
		target.setDestinationWarehouseInfo(desWh);
		// 商品信息
		List<AllocationOrderMerchandiseVo> merchandiseInfoList = new ArrayList<AllocationOrderMerchandiseVo>();
		JSONArray array = json.getJSONArray("merchandiseList");
		for (Object item : array) {
			JSONObject obj = (JSONObject) item;
			AllocationOrderMerchandiseVo merchandiseVo = new AllocationOrderMerchandiseVo();
			merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));
			merchandiseVo.setSpecification(obj.getString("specification"));
			merchandiseVo.setEstimateDispatchQty(obj
					.getInteger("estimateDispatchQty"));
			merchandiseVo.setBoxQty(obj.getInteger("boxQty"));
			merchandiseVo.setRecommendQty(obj.getInteger("suggestQty"));
			merchandiseInfoList.add(merchandiseVo);
		}
		target.setAllocationOrderMerchandiseVos(merchandiseInfoList);
		target.setOrderNo(json.getString("orderNo"));
		target.setOutBoundOrderNo(json.getString("winitOrderNo"));
		return target;
	}

	public static AllocationOrderVo buildFirstStep(JSONObject jsons) {
		JsonValid json = new JsonValid(jsons);
		String winitProductCode = json.getString("winitProductCode");
		AllocationOrderVo vo = new AllocationOrderVo();
		vo.setOrderNo(json.getString("orderNo"));
		ProductInfo productInfo = new ProductInfo();
		productInfo.setWinitProductCode(winitProductCode);
		productInfo.setWinitProductCategory3(json.getString("orderType"));
		vo.setProductInfo(productInfo);
		vo.setOrderType(json.getString("orderType"));
		// 发货仓库信息
		WarehouseInfo disWh = new WarehouseInfo();
		disWh.setWarehouseCode(json.getString("dispatchWhCode"));
		vo.setDispatchWarehouseInfo(disWh);
		// 目的仓库信息
		WarehouseInfo desWh = new WarehouseInfo();
		desWh.setWarehouseCode(json.getString("destinationWhCode"));
		vo.setDestinationWarehouseInfo(desWh);

		CustomerInfo customerInfo = new CustomerInfo();
		customerInfo.setCustomerCode(json.getString("customerCode"));
		vo.setCustomerInfo(customerInfo);
		// 订单来源
		vo.setOrderSource(OrderSource.MO.getSource());
		vo.setRemark(json.getString("remark")); 
		return vo;
	}

	/**
	 * 校验创建调拨单参数 第二步：基本信息校验 继续（校验字段是否必填） 校验是否为空 saveSecondStep
	 * 
	 * @param json
	 */
	public static AllocationOrderVo buildSecondStep(JSONObject jsons) {
		JsonValid json = new JsonValid(jsons);
		AllocationOrderVo vo = new AllocationOrderVo();
		vo.setOrderNo(json.getString("orderNo"));
		List<AllocationOrderMerchandiseVo> merchandiseInfoList = new ArrayList<AllocationOrderMerchandiseVo>();
		JSONArray array = json.getJSONArray("merchandiseList");
		for (Object item : array) {
			JSONObject obj = (JSONObject) item;
			AllocationOrderMerchandiseVo merchandiseVo = new AllocationOrderMerchandiseVo();
			merchandiseVo.setId(obj.getLong("id"));
			merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));
			merchandiseVo.setSpecification(obj.getString("specification"));
			merchandiseVo.setBoxQty(obj.getInteger("boxQty"));
			merchandiseVo.setExpectQty(obj.getInteger("expectQty"));
			merchandiseVo.setRecommendQty(obj.getInteger("suggestQty"));
			merchandiseVo.setEstimateDispatchQty(obj.getInteger("qty"));
			merchandiseInfoList.add(merchandiseVo);
		}
		vo.setAllocationOrderMerchandiseVos(merchandiseInfoList);
		CustomerInfo customerInfo = new CustomerInfo();
		customerInfo.setCustomerCode(json.getString("CustomerCode"));
		vo.setCustomerInfo(customerInfo);
		return vo;
	}

	public static AllocationOrderVo buildThirdStep(JSONObject jsons) {
		JsonValid json = new JsonValid(jsons);
		AllocationOrderVo vo = new AllocationOrderVo();
		vo.setOrderNo(json.getString("orderNo"));
		return vo;
	}

	/**
	 * 构建导出订单列表
	 * 
	 * @param vo
	 * @return
	 */
	public static Map<String, Object> buildExportOrderList(AllocationOrderVo vo) {
		Map<String, Object> map = buildAllocationOrderListPageVo(vo);
		CustomerInfo customerInfo = vo.getCustomerInfo();
		if (customerInfo != null) {
			map.put("customerCode", customerInfo.getCustomerCode());
			map.put("customerName", customerInfo.getCustomerName());
		}
		// 调试暂时写死
		// AllocationOrderRevenueVo revenueVo =
		// vo.getAllocationOrderRevenueVo();
		// if (revenueVo == null) {
		// return map;
		// }
		// map.put("chargeWeight", revenueVo.getChargeWeight());
		// map.put("chargeType", revenueVo.getWay());
		// map.put("orderDeliveryFee", revenueVo.getAmount());
		// map.put("deliveryFeeCurrency", "USD");// oms暂时没存
		// map.put("orderHandlingFee", 0);
		// map.put("handlingFeeCurrency", "USD");
		// map.put("balanceExchangeRate", revenueVo.getExRate());
		// map.put("balanceAmmount", revenueVo.getAmount());
		// map.put("deliveryName", "");// 暂时没有
		return map;
	}

	public static AllocationRecommendCommand buildAllocationOrderListCommand(
			JSONObject jsonObj) {
		AllocationRecommendCommand command = new AllocationRecommendCommand();
		JSONArray json = jsonObj.getJSONArray("merchandiseList");
		if (null != json && json.size() > 0) {
			List<AllocationRecommendVo> recommendVoList = new ArrayList<AllocationRecommendVo>();
			for (Object item : json) {
				AllocationRecommendVo commandVo = new AllocationRecommendVo();
				JSONObject obj = (JSONObject) item;
				commandVo.setMerchandiseCode(obj.getString("merchandiseCode"));
				commandVo.setSpecification(obj.getString("specification"));
				commandVo.setSuggestQty(obj.getString("suggestQty").equals("") ? 0
						: Integer.parseInt(obj.getString("suggestQty")));
				commandVo.setQty(obj.getString("qty").equals("") ? 0
						: Integer.parseInt(obj.getString("qty")));
				commandVo.setBoxQty(obj.getString("boxQty").equals("") ? 0 : Integer
						.parseInt(obj.getString("boxQty")));
				commandVo.setType(obj.getString("type"));
				commandVo.setIsUrgent(obj.getString("isUrgent"));
				commandVo.setMerchandiseSerno(obj.getString("merchandiseSerno"));
				recommendVoList.add(commandVo);
				
			}
			command.setWarehouseCode(jsonObj.getString("warehouseCode"));
			command.setRecommendVoList(recommendVoList);
		}
		return command;
	}

	public static List<Map<String, Object>> buildResultMap(List<AllocationOrderVo> orderList) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if (null != orderList && orderList.size() > 0) {
			for (AllocationOrderVo vo : orderList) {
				Map<String, Object> rsMap = new HashMap<String, Object>();
				rsMap.put("documenType", vo.getDocumentType());
				rsMap.put("originWarehouseName", vo.getDispatchWarehouseInfo()
						.getWarehouseName());
				rsMap.put("destinationWarehouseName", vo
						.getDestinationWarehouseInfo().getWarehouseName());
				rsMap.put("volume", vo.getEstimateVolume());
				rsMap.put("totalAmount",
						vo.getEstimatedTotalCost() == BigDecimal.ZERO ? "N"
								: vo.getEstimatedTotalCost());
				rsMap.put("feeDetail", buildDetail(vo.getFeeEstimateVos()));
				list.add(rsMap);
			}
		}
		return list;
	}

	private static List<Map<String, Object>> buildDetail(
			List<AllocationOrderFeeEstimateVo> feeEstimateVos) {
		List<Map<String, Object>> detail = new ArrayList<Map<String, Object>>();
		if (null != feeEstimateVos && feeEstimateVos.size() > 0) {
			for (AllocationOrderFeeEstimateVo feeVo : feeEstimateVos) {
				Map<String, Object> detailMap = new HashMap<String, Object>();
				detailMap.put("amountName", feeVo.getChargeName());
				detailMap.put("amount", feeVo.getAmount());
				detailMap.put("currency", feeVo.getCurrency());
				detail.add(detailMap);
			}
		}
		return detail;
	}

	public static List<Map<String, Object>> buildResultList(
			List<OrderSubmitResultOrderVo> orderResultList) {
		if (null==orderResultList||orderResultList.size()<1) {
			return null;
		}
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		for (OrderSubmitResultOrderVo vo : orderResultList) {
			Map<String, Object> detailMap = new HashMap<String, Object>();
			detailMap.put("orderNo", vo.getOrderNo());
			detailMap.put("orderType", vo.getOrderType());
			detailMap.put("originWarehouseName", vo.getOriginWarehouseName());
			detailMap.put("destinationWarehouseName",
					vo.getDestinationWarehouseName());
			detailMap.put("errorCode", vo.getErrorCode());
			detailMap.put("volume", vo.getVolume());
			detailMap.put("execResult", vo.getExecResult());
			detailMap.put("causeOfFailure", vo.getCauseOfFailure());
			detailMap.put("submitResultMerchandiseVoList",
					buildsuMitResultMerchandList(vo
							.getSubmitResultMerchandiseVoList()));
			mapList.add(detailMap);
		}
		return mapList;
	}

	private static List<Map<String, Object>> buildsuMitResultMerchandList(
			List<OrderSubmitResultMerchandiseVo> submitResultMerchandiseVoList) {
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		if (null==submitResultMerchandiseVoList||submitResultMerchandiseVoList.size()<1) {
			return null;
		}
		for (OrderSubmitResultMerchandiseVo vo : submitResultMerchandiseVoList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("merchandiseSerno", vo.getMerchandiseSerno());
			map.put("merchandiseCode", vo.getMerchandiseCode());
			map.put("nameCn", vo.getNameCn());
			map.put("specification", vo.getSpecification());
			map.put("qty", vo.getQty());
			map.put("causeOfFailure", vo.getCauseOfFailure());
			map.put("execResult", vo.getExecResult());
			maps.add(map);
		}
		return maps;
	}
	
	/**
	 * 
	 * 构建第一步越库信息.
	 * @param json
	 * @return
	 */
    public static AllocationOrderVo buildCrossDockFirstStep(JSONObject json) {
        AllocationOrderVo vo = new AllocationOrderVo();
        // 调拨单号
        vo.setOrderNo(json.getString("orderNo"));
        
        // 入库单号
        vo.setInboundOrderNo(json.getString("inboundOrderNo"));
        
        vo.setMarkUp(json.getString("markUp"));
        
        // 客户编码
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCustomerCode(CommandContext.getContext().getUserId());
        vo.setCustomerInfo(customerInfo);
        
        
        // 订单来源
        vo.setOrderSource(OrderSource.MO.getSource());
        
        // 
        
        // 构建商品信息
        buildCrossDockMerList(json, vo);
        return vo;
    }
    
    /**
     * 构建越库商品信息
     * 
     * @param json
     */
    public static AllocationOrderVo buildCrossDockMerList(JSONObject jsons, AllocationOrderVo vo) {
        JsonValid json = new JsonValid(jsons);
        List<AllocationOrderMerchandiseVo> merchandiseInfoList = new ArrayList<AllocationOrderMerchandiseVo>();
        JSONArray array = json.getJSONArray("merchandiseList");
        for (Object item : array) {
            JSONObject obj = (JSONObject) item;
            AllocationOrderMerchandiseVo merchandiseVo = new AllocationOrderMerchandiseVo();
            merchandiseVo.setId(obj.getLong("id"));
            merchandiseVo.setMerchandiseCode(obj.getString("merchandiseCode"));
            merchandiseVo.setSpecification(obj.getString("specification"));
            merchandiseVo.setExpectQty(obj.getInteger("expectQty"));
            merchandiseVo.setRecommendQty(obj.getInteger("suggestQty"));
            merchandiseVo.setEstimateDispatchQty(obj.getInteger("qty"));
            merchandiseInfoList.add(merchandiseVo);
        }
        vo.setAllocationOrderMerchandiseVos(merchandiseInfoList);
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setCustomerCode(json.getString("customerCode"));
        vo.setCustomerInfo(customerInfo);
        return vo;
    }
}
